package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.NetSongList;

import java.util.List;

/**
 * Created by yaolin on 15/11/26.
 */
public class HotMenuSongListAdapter extends RecyclerView.Adapter<HotMenuSongListAdapter.MyViewHolder> {
    private List<NetSongList> datas;
    private Context context;
    private setOnClickListener listener;




    public interface setOnClickListener{
        void onItemClick(NetSongList list, int position);
    }

    public void setListener(setOnClickListener l){
        this.listener = l;
    }




    public HotMenuSongListAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<NetSongList> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.list_song, null);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(datas != null && datas.size() > 0 ){
            NetSongList n = datas.get(position);
            holder.music_title.setText(n.getName());
            holder.music_artist.setText(n.getSingerName());
            holder.position = position;
            for (int i = 0; i < datas.size(); i++) {
            holder.music_position.setText( position + 1 + " ");
            }

        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class  MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView music_title,music_artist, music_position;
        private LinearLayout linearLayout;
        private int position;

        public MyViewHolder(View itemView) {
            super(itemView);


            music_title = (TextView) itemView.findViewById(R.id.music_title);
            music_artist = (TextView) itemView.findViewById(R.id.music_artist);
            music_position = (TextView)  itemView.findViewById(R.id.music_position);

            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            linearLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(datas != null &&  datas.size() > 0){
                NetSongList n  = datas.get(position);
                if(listener != null){
                    listener.onItemClick(n, position);
                }
            }
        }
    }
}
